package cn.com.nxt.txb;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.com.nxt.txb.adapter.BusinessNumberAdapter;
import cn.com.nxt.txb.httpdelegate.BusinessDelegate;
import cn.com.nxt.txb.model.BusinessNumber;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.MyItemDecoration;
import cn.com.nxt.txb.util.OnRecyclerItemClickListener;
import cn.com.nxt.txb.widget.ClearEditText;

@ContentView(R.layout.activity_choose_business)
public class ChooseBusinessActivity extends BaseActivity {
    private byte BUSINESS_INFO_FLAG = 0x00;
    private byte BUSINESS_NUMBER_LIST_FLAG = 0x01;
    @ViewInject(R.id.et_phone)
    private ClearEditText mPhoneEt;
    @ViewInject(R.id.rv)
    private RecyclerView rv;
    private String phone;
    private BusinessDelegate businessDelegate;
    private BusinessNumberAdapter mAdapter;
    private List<BusinessNumber> list;
    private BusinessNumber currentNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        businessDelegate = new BusinessDelegate(this);
        if (SampleApplicationLike.isNotBusniessUser) {
            showToast("无企业权限");
            finish();
            return;
        }
        if (TextUtils.isEmpty(SampleApplicationLike.businessName) || TextUtils.isEmpty(SampleApplicationLike.businessID)) {
            businessDelegate.businessInfo(AppUtil.mUser.getToken(), BUSINESS_INFO_FLAG);
        } else {
            businessDelegate.getBusinessNumberList(SampleApplicationLike.businessID, AppUtil.mUser.getToken(), BUSINESS_NUMBER_LIST_FLAG);
        }
        rv.setLayoutManager(new LinearLayoutManager(ChooseBusinessActivity.this));
        rv.addItemDecoration(new MyItemDecoration());
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        disDialog();
        if (flag == BUSINESS_INFO_FLAG)
            businessInfo(result);
        else if (flag == BUSINESS_NUMBER_LIST_FLAG)
            businessNumberList(result);
    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {
        super.failure(result, flag, status);
        disDialog();
    }

    private void businessInfo(String result) {
        if (SampleApplicationLike.isNotBusniessUser) {
            showToast("无企业权限");
            finish();
            return;
        }
        JSONObject obj = null;
        try {
            obj = new JSONObject(result);
            SampleApplicationLike.businessName = obj.optString("name");
            SampleApplicationLike.businessID = obj.optString("bsid");
            showDialog();
            businessDelegate.getBusinessNumberList(SampleApplicationLike.businessID, AppUtil.mUser.getToken(), BUSINESS_NUMBER_LIST_FLAG);
        } catch (JSONException e) {
        }
    }

    private void businessNumberList(String result) {
        try {
            list = new ArrayList<>();
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++) {
                Gson gson = new Gson();
                BusinessNumber number = gson.fromJson(array.getString(i), BusinessNumber.class);
                list.add(number);
            }
            mAdapter = new BusinessNumberAdapter(ChooseBusinessActivity.this, list);
            mAdapter.setOnItemClickListener(mOnItemClickListener);
            rv.setAdapter(mAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Event({R.id.tv_commit})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.tv_commit:
                commitPhone();
                break;
        }
    }

    private void commitPhone(){
        phone = mPhoneEt.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("手机号不能为空！");
            return;
        }
        if (!AppUtil.isPhoneNumberValid(phone)) {
            showToast("手机号格式错误！");
            return;
        }
        startResult(phone);
    }

    private OnRecyclerItemClickListener mOnItemClickListener = new OnRecyclerItemClickListener() {
        @Override
        public void OnItemClick(View v, int pos) {
            currentNumber=list.get(pos);
            if (currentNumber.getPhone().startsWith("1")) {
                if (currentNumber.getPhone().equals(AppUtil.mUser.getPhone())) {
                    showToast("不能邀请自己！");
                    return;
                }
                startResult(currentNumber.getPhone());
            }
        }
    };

    private void startResult(String phone){
        Intent intent = new Intent();
        intent.putExtra("phone", phone);
        setResult(InviteMeetingActivity.RESULT_BUSINESS, intent);
        finish();
    }
}
